proc remchan {nick chan otbn1 handle host} {
  global botnick home bcast botadmin 

  # do not allow removal of the home channel(s)
  if {($otbn1 == "$home")  ||  (![matchattr $handle n])} {
    # Bug fixed - allowed removal of home channel in OTBN 2
    putquick "NOTICE $nick :\0034ERROR - This bot may not leave the home channel.  Please contact $botadmin to have this setting changed."

    if {$bcast == "1"} {
      putquick "PRIVMSG $home :\002$nick\002 ($host) tried to remove \002$home\002 - The current home channel on me."
    }

    return 0
  # if no channel is specified
  } elseif {$otbn1 == ""} {
    putquick "NOTICE $nick :\0034ERROR - Should Be - (/msg) $botnick remchan <#channel>"
    return 0
  # if no # in channel name
  } elseif {![string match "#*" $otbn1]} {
    putquick "NOTICE $nick :\0034ERROR - Illegal Channel Name - Try adding a # in front of it (eg. #overtech)"
    return 0
  # if bot is not in the channel, don't remove it
  } elseif {![validchan $otbn1]} {
    putquick "NOTICE $nick :\0034ERROR - Cannot remove $botnick from a channel that is not being serviced or occupied by $botnick."
    return 0
  }

  putlog "!$nick ($host)! remchan $otbn1"
  putquick "NOTICE $nick :\0033Sucessfully removed $otbn1 from channel partition."


  if {$bcast == "1"} {
    putquick "PRIVMSG $home :\002$nick\002 ($host) requested me to remchan \002$otbn1\002 from my channel partition."
  }

  # Remove all users currently known for that channel
  foreach user [userlist]  {
    set userdeluser 1
    if {[delchanrec $user $otbn1]} {
      save

      if {![matchattr $user f]} {
        foreach channels [channels] {

          if {[matchattr $user |f $channels]} {
            set userdeluser 0
          }
        }

        if {$userdeluser} {
          deluser $user
          channel remove $otbn1
          save		
          return 0
        }		
      }
    }
  }

  channel remove $otbn1
  save
}

